# -*- coding: utf-8 -*-
"""
V3版本改进：
1. B任务：只添加B任务，默认禁止下载功能
2. 紧急任务： 所有更新的紧急任务都会加入到我的任务列表中，不根据设定的数据库过滤
3. 添加监视线程，负责紧急任务添加四个小时之后，继续自动添加该任务
"""
import re,os
from core import opener
from core import task_types, login, get_salary
from Threads import *
import var
import time

def add_tasks(task_type):
    """
    添加任务
    """
    task_url = "http://www.firstdrs.com/admin/" + task_types[task_type]
    html = opener.open(task_url).read()
    if 'safeCode.png' in html:
        print 'Restricted, try again later!'
        with open('asdf.html','w') as f:
            f.write(html)
        return
    #with open('asdf.html','w') as f:
    #    f.write(html)
    templist = re.findall(r'<a href = "\?id=(\d+).*?">.*?</a>',html)
    #print templist
    for each in templist:
        # 将所有id放入数据库检测队列
        q_db.put((each,task_type))
        #chkdb_threads.push((each,task_type))

def filter_tasks():
    """
    删除不含有效链接的任务
    """
    tasks = get_mytasks()
    for each in tasks:
        q_filter.put(each)

def delete_tasks():
    """
    删除我的所有任务
    """
    tasks = get_mytasks()
    for each in tasks:
        del_task(each)

#@safecode.png
def get_mytasks():
    """
    获取我的任务列表
    """
    mytask_url = "http://www.firstdrs.com/admin/first/searchnosend.do"
    html = opener.open(mytask_url).read()
    if 'safeCode.png' in html:
        print 'Restricted, try again later!'
        return []
    return re.findall(r'<a href = "quicksend.do\?id=(\d+)',html)

#def is_restricted(func):
    
    
            
if __name__ == '__main__':
    login()
    
    choice = raw_input('''
Please choose operation:
0. Add task A and B
1. Add task B and filter the invalid tasks
2. Add task IMPORTANT and filter the invalid tasks
3. Add task A and filter the invalid tasks
4. Filter all my tasks and download
5. View my salary
''')
    var.f = open('record.txt','a+')
    var.ids = [ each.strip() for each in var.f.readlines()]
    start_threads()
    if choice == '0':
        while True:
            try:
                add_tasks('b')
                time.sleep(5)
                add_tasks('a')
                time.sleep(15)
            except Exception,e:
                print e
    if choice == '1':
        while True:
            try:
                add_tasks('b')
                time.sleep(10)
            except Exception,e:
                print e
    if choice == '3':
        while True:
            try:
                add_tasks('a')
                time.sleep(10)
            except Exception,e:
                print e
    if choice == '2':
        while True:
            try:
                add_tasks('important')
                time.sleep(10)
            except Exception,e:
                print e
    if choice == '4':
        var.auto_download = True
        if not os.path.exists('pdf'):
            os.mkdir('pdf')
        filter_tasks()
    if choice == '5':
        get_salary()
        time.sleep(10)

    q_filter.join()
    q_db.join()
    download_queue.join()
